<?php
namespace app\wechat\controller;

use app\common\base\BasicRest;
use app\wechat\model\UserInfoModel;
use app\component\controller\Wechatlogin;
use think\Config;
use think\Db;
use think\Request;
use think\Session;

class Login extends  BasicRest
{


    // protected $beforeActionList = [
    //       'islogin'
    //   ];

    public function __construct()
    {
        parent::__construct();
    }
    // //判断是否登陆
    // protected function islogin(){
    //     if(!Session::has('userid')){
    //         return $this->redirect('/wechat/login/login',302);
    //     }
    // }
        

   public function login(){
        // return $this->redirect('/wechat/login/openidlogin?openid='.$openid,302);
        $wechatlogin=new Wechatlogin();
        // var_dump($this->request->domain()."/wechat/login/openidlogin.html");
        // exit;
        $wechatlogin->Wechatlogin(urlencode($this->request->domain()."/php/public/wechat/login/openidlogin.html")); 
   }

   public function openidlogin(){
        $wechatlogin=new Wechatlogin();
        $data=$this->request->get();
        $openiddata=$wechatlogin->wechatgetopenid();

        $openid=$openiddata['openid'];
        
        //查询是否有这个人
        $user=UserInfoModel::get(['open_id'=>$openid]);
        if($user){
            $userid=$user->id;
        }else{
            $user = new UserInfoModel();
            $user->open_id=$openid;
            $user->is_draw=0;
            $user->is_check=0;
            $user->save();
            $userid=$user->id;
        }
        Session::set('id',str_replace("_","-",$openid));
        Session::set('userid',$userid);
        echo "<script>window.location.href='/php/public/wechat/qiandao/qiandao';</script>";
        exit;
   }
}